Imports System.Math
Imports System.Double

Public Class Form1
  Inherits System.Windows.Forms.Form

  ' Kod generowany przez Windows Form Designer 


  Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnCalc.Click
    Dim Values() As Double, Mean As Double, SD As Double
    Dim N As Long, i As Integer, Which As Integer

    txtMean.Enabled = False
    txtSD.Enabled = False

    N = CLng(txtN.Text)   ' Ile wartoci wygenerowa

    ReDim Values(N)       ' Ustawienie rozmiaru tablic

    Randomize()           ' Uruchomienie generatora liczb losowych

    For i = 0 To N - 1
      Values(i) = 101.0 * Rnd()  ' Korekta niektrych wartoci
    Next

    If rbPopulation.Checked = True Then
      Which = 1   ' Oblicz odchylenie standardowe populacji
    Else
      Which = 0   ' Oblicz odchylenie standardowe prbki
    End If

    SD = StandardDeviation(Values, Mean, N, Which)
    If Not IsInfinity(Mean) And Not IsNaN(SD) Then
      txtMean.Enabled = True
      txtSD.Enabled = True
    End If
    txtSD.Text = Format(SD, "###.#####")
    txtMean.Text = Format(Mean, "###.#####")

  End Sub

  Public Function StandardDeviation(ByVal X() As Double, ByRef Mean As _
          Double, ByVal N As Double, ByVal Which As Double) As Double
    ' Cel: ta funkcja oblicza redni i odchylenie standardowe 
    '      serii danych.
    '
    ' Lista argumentw:
    '  X()    tablica typu double, ktra zawiera dane
    '  Mean   wyliczona rednia, ktra ma by wyznaczana przez t funkcj
    '  N      liczba obserwacji
    '  Which  oblicza odchylenie standardowe populacji (Which = 1) 
    '         lub odchylenie standardowe prbki (Which = 0)
    '
    ' Zwracana warto:
    '  Double odchylenie standardowe dla zbioru danych. Jeli zostanie
    '         wykryty bd, odchylenie standardowe jest ustawiane na NaN, 
    '         a Mean jest ustawiane na plus lub minus nieskoczono.
    '
    ' UWAGA: Zauwa, e argument Mean jest przekazany przez 
    '        odwoanie (ByRef)

    Dim i As Long, sum As Double, ss As Double, SD As Double
    Dim Term As Double

    Try
      sum = 0.0   ' Tak, wiem... i tak s ustawione na 0. 
      ss = 0.0
      For i = 0 To N - 1
        sum += X(i)        ' Tworzy sum 
        ss += X(i) * X(i)  ' Sumuje kwadraty 
      Next

      Mean = sum / CDbl(N)   ' Oblicza redni

      If Which = 1 Then      ' Odchylenie standardowe populacji
        Term = CDbl(N)
      Else                   ' Odchylenie standardowe prbki, traci
                                   ' jeden stopie swobody
        Term = CDbl(N - 1.0)
      End If

      If IsPositiveInfinity(Mean) Or IsNaN(Mean) Then
        Throw New DivideByZeroException()
      End If

    Catch e As DivideByZeroException  ' Jeli N = 0
      sum = PositiveInfinity

    Catch e As OverflowException      ' Jeli to dua warto
      sum = PositiveInfinity

    Catch
      sum = NegativeInfinity        ' Dla pozostaych przypadkw

    Finally
      SD = Sqrt(((N * ss) - (sum * sum)) / (N * Term))
      If IsInfinity(SD) Or IsNaN(SD) Then
        SD = NaN
        Mean = PositiveInfinity
      End If
    End Try
    Return SD
  End Function

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnExit.Click
    Me.Dispose()
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
                System.EventArgs) Handles MyBase.Load
    rbSample.Checked = True
    txtmean.Enabled = False
    txtSD.Enabled = False

  End Sub
End Class
